package imitate_Stack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-15
 * Time: 16:49
 */
public class MyStack {
    private int[] elem;
    private int useSize ;
    public int Initial_value = 5;
    public MyStack() {
        this.elem = new int[Initial_value];
    }
    //压栈
    public void push(int val) {
        if(isFull()) {
            Expansion();
        }
        elem[useSize] = val;
        useSize ++;
    }
    //扩容
    public void Expansion() {
        elem = Arrays.copyOf(elem, 2 * elem.length);
    }
    public boolean isFull(){
        return useSize == elem.length;
    }
    public boolean isEmpty()    {
        return useSize == 0;
    }
    public int pop() {
        if (isEmpty()) {
            throw new StackEmptyException("栈为空！！！");
        }
        return elem[--useSize];
    }
    public int peek() {
        if (isEmpty()) {
            throw new StackEmptyException("栈为空！！！");
        }
        return elem[useSize - 1];
    }
}